/* -*-c++-*- */
/* osgGIS - GIS Library for OpenSceneGraph
 * Copyright 2007-2008 Glenn Waldron and Pelican Ventures, Inc.
 * http://osggis.org
 *
 * osgGIS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

#ifndef _OSGGIS_RTREE_H_
#define _OSGGIS_RTREE_H_ 1

#include <osgGIS/Common>
#include <osgGIS/GeoExtent>
#include <vector>
#include <list>
#include <map>
#include <float.h>
#include <iostream>
#include <sstream>
#include <iomanip>

// Simple block-based R-Tree implementation. It is block-based so that we can easily
// adapt it to use a disk-block backing store in the future.
// Spec: http://www.baymoon.com/~tg2/rtrees.pdf

namespace osgGIS
{
    /* Unique identifier for an rtree node. */
    typedef int RtNodeId;
    
    /* Single entry in either a leaf or non-leaf node */
    template<typename DATA> class RtEntry
    {
    public:
        inline RtEntry() : child_id(0) { }        
        GeoExtent extent;
        union {
            RtNodeId child_id;
            DATA data;
        };        
    };
    
    /* Ordered collection of node entries */
    template<typename DATA> class RtEntryList : public std::vector<RtEntry<DATA> > { };
    
    /* An R-tree node */
    template<typename DATA> class RtNode : public osg::Referenced
    {
    public:
        inline RtNode() : id(0), parent_id(0), is_leaf(true) { }
        inline bool isLeaf() { return is_leaf; }
        inline bool isRoot() { return parent_id == 0; }
        
    public:
        RtNodeId id;
        RtNodeId parent_id;
        bool is_leaf;
        RtEntryList<DATA> entries;
    };    
    
    /* table that maps node IDs to nodes (for caching/in-memory storage) */
    template<typename DATA> class RtNodeMap : public std::map<RtNodeId, osg::ref_ptr<RtNode<DATA> > > { };

    /* An R-tree spatial data structure */
    template<typename DATA> class RTree : public osg::Referenced
    {
    public:
        inline RTree();
        inline void insert( const GeoExtent& extent, const DATA& data );
        
        inline std::list<DATA> find( const GeoExtent& extent );

        inline bool writeTo( std::ostream& out, const GeoExtent& extent );
        inline bool readFrom( std::istream& in, SpatialReference* srs, GeoExtent& out_extent );

    private:
        inline void insert( const RtNodeId&, const GeoExtent&, const DATA& );
    
    
        inline RtNode<DATA>* chooseLeaf( const RtNodeId& node_id, const GeoExtent& extent );
        inline void          adjustTree( RtNode<DATA>* L, RtNode<DATA>* LL );
        inline RtNode<DATA>* splitNodeQ( RtNode<DATA>* L );
        inline void          find( const RtNodeId& node_id, const GeoExtent& extent, std::list<DATA>& result);
        
        
        inline void pickSeedsQ( 
            RtEntryList<DATA>& list,
            int&               out_first_index,
            int&               out_second_index);
        
    private:
        RtNodeMap<DATA> node_table;
        RtNodeId root_id;
        RtNodeId node_id_gen;                
    };
    
#include "RTree.cpp.inline"
}


#endif // _OSGGIS_RTREE_H_
